package com.xxx.auth.common.util;

import com.xxx.auth.entity.Permission;
import com.xxx.auth.entity.PermissionCategory;
import com.xxx.auth.entity.enums.PermissionType;
import com.xxx.auth.repository.PermissionCategoryRepository;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限相关的操作
 */
public class PermissionUtil {

    /**
     * 创建或更新权限目录
     *
     * @param fileName                     权限文件名
     * @param permissionCategoryRepository repository
     * @return 权限目录
     */
    public static Map<String, PermissionCategory> createPermissionCategory(String fileName, PermissionCategoryRepository permissionCategoryRepository) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream(), "utf-8"));
            String line;

            Map<String, PermissionCategory> permissionCategoryMap = new HashMap<>();
            while ((line = bufferedReader.readLine()) != null) {
                if (StringUtils.hasText(line) && !line.startsWith("#")) {
                    String[] values = line.split(" ");
                    String name = values[0];
                    String description = values[1];
                    PermissionCategory permissionCategory = permissionCategoryRepository.findByName(name)
                            .orElseGet(() -> {
                                PermissionCategory newCategory = new PermissionCategory().setName(name).setDescription(description);
                                permissionCategoryRepository.save(newCategory);
                                return newCategory;
                            });
                    permissionCategoryMap.put(name, permissionCategory);
                }
            }
            return permissionCategoryMap;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 角色的权限列表
     *
     * @param fileName                     权限文件名
     * @param permissionCategoryRepository repository
     * @return 权限列表
     */
    public static List<Permission> getPermissionList(String fileName, PermissionCategoryRepository permissionCategoryRepository) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream(), "utf-8"));
            String line;

            Map<String, PermissionCategory> permissionCategoryMap = createPermissionCategory("permission/permission_category.txt", permissionCategoryRepository);
            List<Permission> permissionList = new ArrayList<>();
            PermissionCategory currentCategory = null;

            while ((line = bufferedReader.readLine()) != null) {
                if (StringUtils.hasText(line)) {
                    if (line.startsWith("#")) {
                        String categoryName = line.split(" ")[1].trim();
                        currentCategory = permissionCategoryMap.get(categoryName);
                    } else {
                        String[] values = line.split(" ");
                        String name = values[0];
                        String description = values[1];
                        PermissionType permissionType = PermissionType.valueOf(values[2]);
                        Permission permission = new Permission().setName(name).setDescription(description).setPermissionType(permissionType);
                        if (currentCategory != null) {
                            permission.setPermissionCategory(currentCategory);
                        }
                        permissionList.add(permission);
                    }
                }
            }
            return permissionList;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
